﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using Newtonsoft.Json;
using Sovigaz.DataProvider;
using Sovigaz.Permission.Repositories;
using Utilities;

namespace Sovigaz.Permission.Api
{
    public class InOutApiController : ApiController
    {
        public InOutRepository Get(int? pageNumber, string orderBy, string orderDirection, string inoutCode, string userFullName, string deliveryPlanCode, string datefrom, string dateto)
        {
            try
            {
                string whereClause = "IsDeleted != true";
                if (!String.IsNullOrWhiteSpace(inoutCode))
                    whereClause = String.Format(whereClause + " AND inoutCode.Contains(\"{0}\")", inoutCode);                

                return new InOutRepository().GetList(whereClause, orderBy, orderDirection, pageNumber ?? 1);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public InOutComplex Get(int? id)
        {
            try
            {
                var item = new InOutComplex();
                var repository = new InOutRepository();
                item.InOutCode = GenerateCode.GetCode(repository.GetNextIndex(), CodeType.InOut);
                item.InOutLines = new List<InOutLineComplex>();
                if (id.HasValue && id > 0)
                {
                    item = repository.GetByID(id.Value);
                    var iolrepository = new InOutLineRepository();
                    item.InOutLines = iolrepository.GetByInOutID(id.Value);
                }
                return item;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public void Delete(int? id)
        {
            try
            {
                if (id.HasValue && id > 0)
                {
                    var repository = new InOutRepository();
                    repository.Delete(id.Value);
                }
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public InOutRepository Delete(string ids, int? pageNumber, string orderBy, string orderDirection, string inoutCode, string userFullName, string deliveryPlanCode, string datefrom, string dateto)
        {
            try
            {
                if (ids.StartsWith(","))
                    ids = ids.Remove(0, 1);
                var idArr = ids.Split(',');
                var repository = new InOutRepository();
                repository.Delete(idArr);

                return Get(pageNumber, orderBy, orderDirection, inoutCode, userFullName, deliveryPlanCode, datefrom, dateto);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        //public InOut Put(InOut item)
        //{
        //    try
        //    {
        //        var repository = new InOutRepository();
        //        int ID = repository.Update(item);

        //        return Get(ID);
        //    }
        //    catch (HttpResponseException)
        //    {
        //        throw;
        //    }
        //    catch (Exception e)
        //    {
        //        throw ExceptionHelper.CreateHttpResponseException(e.Message, e);
        //    }
        //}
    }
}
